package com.example.springbootlog4j.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 新增时间格式工具类
 * @author yihua
 *
 */
@SuppressWarnings("unused")
public final class DateUtil {
    public static void main(String[] args) {
		String nowDate = getNowDate(TIME_FORMAT_MINUTE);
		System.out.println(nowDate);
		long timeStamp = getTimeStamp();
		System.out.println("timeStamp:"+timeStamp);
		String timeStamp2 = date2TimeStamp("20210728", "yyyyMMdd");
		System.out.println(timeStamp2);
		String month = timeStamp2Month(timeStamp);
		System.out.println(month);
		System.out.println(timeStamp2Day(timeStamp));
	}

	// 年月格式
	public static String YEARMONTHFORMAT = "yyyyMM";
	public static String DATEFORMAT = "yyyyMMdd";

	// 短日期格式
	public static String MONTH_FORMAT = "yyyy-MM";

	// 短日期格式
	public static String DATE_FORMAT = "yyyy-MM-dd";
	// 长日期格式
	public static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	// 长日期格式（截至到分钟）
	public static String TIME_FORMAT_MINUTE = "yyyy-MM-dd HH:mm";

	// 长日期格式（截至到小时）
	public static String TIME_FORMAT_HOUR= "yyyy-MM-dd HH";

	/**
	 * 获取当前时间，格式例子如：yyyy-MM-dd 或者yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getNowDate(String format) {
		// 当前时间
		SimpleDateFormat df = new SimpleDateFormat(format);
		String nowdate = df.format(new Date());
		return nowdate;
	}
	/**
	 * 获取前n天的日期
	 * @param num   	获取前num天
	 * @param format	返回的日期格式 格式例子如：yyyy-MM-dd 或者yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getPreDay(int num,String format) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		SimpleDateFormat retursdf=new SimpleDateFormat(format);  
        Date date=new Date();  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, -num);  
        date = calendar.getTime(); 
        String preday=retursdf.format(date);
		return preday;
	}

	/**
	 * 获取前n天的日期
	 * @param num   	获取前num天
	 * @param format	返回的日期格式 格式例子如：yyyy-MM-dd 或者yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getPreDay(Date date,int num,String format) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat retursdf=new SimpleDateFormat(format);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -num);
		date = calendar.getTime();
		String preday=retursdf.format(date);
		return preday;
	}

	/**
	 * 获取前n个月的日期
	 * @param num   	获取前num月
	 * @param format	返回的日期格式 格式例子如：yyyy-MM-dd 或者yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getPreMonth(int num,String format) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");  
		SimpleDateFormat retursdf=new SimpleDateFormat(format);  
        Date date=new Date();  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.MONTH, -num);  
        date = calendar.getTime(); 
        String preday=retursdf.format(date);
		return preday;
	}
	
	
	/**
	 * 当前月的第一天,格式：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getNowMonthFirstDay() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		Date beginTimeDate = cal.getTime();
		String beginTime = df.format(beginTimeDate) + " 00:00:00";
		return beginTime;
	}
	
	
	/**
	 * 当前月的第一天，自定义格式
	 * @return
	 */
	public static String getNowMonthFirstDay(String format) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat(format);
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		Date beginTimeDate = cal.getTime();
		String beginTime = df.format(beginTimeDate) ;
		return beginTime;
	}
	
	/**
	 * 日期转换成星期几
	 * @param datetime 日期：例如：20180613
	 * @author zhangtao
	 * @return String 例如：周三
	 */
	public static String dateToWeek(String datetime) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		String [] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		Date date = null;
		 try {
			date = simpleDateFormat.parse(datetime);
			cal.setTime(date);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		 int w = cal.get(Calendar.DAY_OF_WEEK)-1;//指示一个星期中的某天
		 if(w<0) 
			 w=0;
		 return weekDays[w];
	}

	/**
	 * 时间戳转换成到月
	 * @param timeStamp 时间戳
	 * @return
	 */
	public static String timeStamp2Month(long timeStamp) {
		return timeStamp2Date(timeStamp,"yyyyMM");
	}

	/**
	 * 时间戳转换成到日
	 * @param timeStamp 时间戳
	 * @return
	 */
	public static String timeStamp2Day(long timeStamp) {
		return timeStamp2Date(timeStamp,"yyyyMMdd");
	}

	/**
	 * 时间戳转换成日期格式字符串
	 * @param timeStamp 时间戳
	 * @param format
	 * @return
	 */
	public static String timeStamp2Date(long timeStamp,String format) {
		if(format == null || format.isEmpty()){
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(timeStamp));
	}
	/**
	 * 日期格式字符串转换成时间戳
	 * @param date_str 字符串日期
	 * @param format 如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String date2TimeStamp(String date_str,String format){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return String.valueOf(sdf.parse(date_str).getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 取得当前时间戳（精确到秒）
	 * @return
	 */
	public static long getTimeStamp(){
		long time = System.currentTimeMillis();
		return time;
	}


	public static String DateToString(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date datenew = sdf.parse(date);
			SimpleDateFormat formatter = new SimpleDateFormat(format);
			String dateString = formatter.format(datenew);
			return dateString;
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public static String DateToStringByformat(String date, String format,String toFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date datenew = sdf.parse(date);
			SimpleDateFormat formatter = new SimpleDateFormat(toFormat);
			String dateString = formatter.format(datenew);
			return dateString;
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}




	/**
	 * 两个时间相减得天数
	 * @param beginDateStr
	 * @param endDateStr
	 * @return
	 */
	public static long getDaySub(String beginDateStr,String endDateStr,String dateFormate) {

		long day = 0;
		SimpleDateFormat format = new SimpleDateFormat(dateFormate);
		Date beginDate;
		Date endDate;
		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
			day = (endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return day;
	}

	/**
	 * 获取前n天的日期
	 * @param num   	获取前num天
	 * @param format	返回的日期格式 格式例子如：yyyy-MM-dd 或者yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getAddDay(String dateStr,int num,String format) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat retursdf=new SimpleDateFormat(format);
		Date date= stringToDate(dateStr,"yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, num);
		date = calendar.getTime();
		String preday=retursdf.format(date);
		return preday;


	}
	public static Date stringToDate(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

}
